/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pa165.dominatingspecies.frontend.animal;

import java.util.HashSet;
import java.util.List;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.dao.DataAccessException;
import org.springframework.security.access.prepost.PreAuthorize;
import pa165.dominating.species.tos.AnimalTo;
import pa165.dominating.species.tos.EnvironmentTo;
import pa165.dominatingspecies.frontend.pages.BasePage;
import pa165.dominatingspecies.service.converter.EnvironmentConverter;
import pa165.dominatingspecies.service.impl.AnimalServiceImpl;
import pa165.dominatingspecies.service.impl.EnvironmentServiceImpl;

/**
 *
 * @author Filip Dinh
 */
@PreAuthorize("hasRole('ADMIN')")
public class CreateAnimalPage extends BasePage {

    @SpringBean(name = "animalService")
    private AnimalServiceImpl animalService;
    @SpringBean(name = "environmentService")
    private EnvironmentServiceImpl environmentService;
    private EnvironmentConverter environmentConverter;
    private String species;
    private String name;
    private EnvironmentTo selectedEnvironment;
    private final EnvironmentTo filter;

    public CreateAnimalPage(final EnvironmentTo filter) {
        this.filter = filter;
        Form<CreateAnimalPage> createForm = new Form<>("createAnimalForm",
                new CompoundPropertyModel<>(this));
        add(createForm);
        createForm.add(new Label("nameLabel", new StringResourceModel("animalNameLabel", this, null)));
        createForm.add(new TextField<String>("name"));
        createForm.add(new Label("speciesLabel", new StringResourceModel("animalSpeciesLabel", this, null)));
        createForm.add(new TextArea<String>("species"));
        createForm.add(new Label("environmentsLabel", new StringResourceModel("selectEnvironment", this, null)));

        List<EnvironmentTo> environments = environmentService.getAllEnvironments();
        createForm.add(new DropDownChoice<>("environments", new PropertyModel<EnvironmentTo>(this, "selectedEnvironment"), environments));
        Link<BasePage> backToList = new Link<BasePage>("backToList") {
            @Override
            public void onClick() {
                AnimalsPage ep = new AnimalsPage(filter);
                setResponsePage(ep);
            }
        };
        createForm.add(backToList);

        Button submitButton = new Button("submitButton") {
            @Override
            public void onSubmit() {
                boolean errorsFound = false;
                if (name == null) {
                    getSession().warn(new StringResourceModel("animalNameEmptyWarn", this, null).getString());
                    errorsFound = true;
                }
                if (species == null) {
                    getSession().warn(new StringResourceModel("animalSpeciesEmptyWarn", this, null).getString());
                    errorsFound = true;
                }
                if (selectedEnvironment == null) {
                    getSession().warn(new StringResourceModel("animalNoEnvironmentSelectedWarn", this, null).getString());
                    errorsFound = true;
                }

                if (!errorsFound) {

                    AnimalTo animal = new AnimalTo();
                    animal.setSpecies(species);
                    animal.setName(name);
                    animal.setEnvironmentTo(selectedEnvironment);
                    animal.setPreyTo(new HashSet<AnimalTo>());
                    boolean thrown = false;
                    try {
                        animalService.createAnimal(animal);
                    } catch (DataAccessException e) {
                        getSession().warn(new StringResourceModel("animalCreateFail", this, null).getString());
                        System.out.println(e.getMessage());
                        thrown = true;
                    }
                    if (!thrown) {
                        getSession().info(new StringResourceModel("animalAddedSucc", this, null).getString());
                        setResponsePage(new AnimalsPage(filter));
                    }
                }
            }
        };
        createForm.add(submitButton);
    }

    public String getSpecies() {
        return species;
    }

    public void setSpecies(String species) {
        this.species = species;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public EnvironmentTo getSelectedEnvironment() {
        return selectedEnvironment;
    }

    public void setSelectedEnvironment(EnvironmentTo selectedEnvironment) {
        this.selectedEnvironment = selectedEnvironment;
    }
}
